﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using KeRui.BLL;
using System.Text;
using KeRui.Common;

public partial class Manage_Cms_ModuleItem : System.Web.UI.Page
{
    protected KeRui.Model.TagModule modelTagModule = new KeRui.Model.TagModule();
    protected KeRui.BLL.Common bllCommon = new Common();
    protected KeRui.BLL.TagModule bllTagModule = new TagModule();
    protected string btnStr = "生成";
    protected string btnVisible = "visible";
    protected string InfoView = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        //加载数据列表
        KeRui.BLL.TagModule bllTagModule = new TagModule();
        switch (PageRequest.GetString("act"))
        {
            case "modify":
                modelTagModule = bllTagModule.GetModel(PageRequest.GetInt("id", -1));
                btnStr = "修改";
                btnVisible = "visible";
                break;
            case "view":
                modelTagModule = bllTagModule.GetModel(PageRequest.GetInt("id", -1));
                btnStr = "修改";
                ParameterView(PageRequest.GetString("name"));
                btnVisible = "hidden";
                break;
            default: break;
        }
        if (modelTagModule.Direction == null)
        {
            modelTagModule.Direction = "DESC";
            modelTagModule.TopNum = 100;
        }
    }

    protected string BuildDataTableList(string selected)
    {
        StringBuilder sb = new StringBuilder();
        KeRui.BLL.TagModule bllTagModule = new TagModule();
        DataTable dtDBList = bllTagModule.GetDBList();
        foreach (DataRow row in dtDBList.Rows)
        {
            if (row["name"].ToString() == selected)
            {
                sb.AppendFormat("<option selected='selected' value='{2}'>{0}-{1}</option>", row["name"], row["value"], row["id"]);
            }
            else
            {
                sb.AppendFormat("<option  value='{2}'>{0}-{1}</option>", row["name"], row["value"], row["id"]);
            }
        }
        return sb.ToString();
    }

    protected string BuildDataFieldsList(string field)
    {
        StringBuilder sb = new StringBuilder();
        KeRui.BLL.TagModule bllTagModule = new TagModule();
        DataTable dtDBList;
        switch (PageRequest.GetString("act"))
        {
            case "view":
            case "modify":
                dtDBList = bllTagModule.GetFieldsByTableID(Convert.ToInt32(bllTagModule.GetTableIDByTableName(bllTagModule.GetModel(PageRequest.GetInt("id", -1)).DataTable)));
                break;
            default:
                dtDBList = bllTagModule.GetFieldsByTableID((int)bllTagModule.GetDBList().Rows[0]["id"]);
                break;
        }
        foreach (DataRow row in dtDBList.Rows)
        {
            if (row["name"].ToString() == field)
            {
                sb.AppendFormat("<option selected='selected' value='{0}'>{0}-{1}</option>", row["name"], row["value"]);
            }
            else
            {
                sb.AppendFormat("<option value='{0}'>{0}-{1}</option>", row["name"], row["value"]);
            }
        }
        return sb.ToString();
    }

    #region 模块信息预览
    /// <summary>
    /// 模块信息预览
    /// </summary>
    /// <param name="parameter">模块名称</param>
    /// <returns></returns>
    protected void ParameterView(string parameter)
    {
        parameter = parameter.Replace("parameter_", string.Empty);        
        DataRow dataRow = bllTagModule.GetModelByName(parameter).Rows[0];
        string tablename = dataRow["DataTable"].ToString();
        string top = dataRow["TopNum"].ToString() == "0" ? "*" : dataRow["TopNum"].ToString();
        string where = dataRow["StrWhere"].ToString();
        string order = dataRow["OrderBy"].ToString();
        string direction = dataRow["Direction"].ToString();
        string sql;
        if (top == "*")
        {
            if (string.IsNullOrEmpty(where))
            {
                sql = string.Format("SELECT  ROW_NUMBER() OVER(ORDER BY {1} DESC) AS rowscount ,* FROM {0} ORDER BY {1} {2}", tablename, order, direction);
            }
            else
            {
                sql = string.Format("SELECT   ROW_NUMBER() OVER(ORDER BY {1} DESC) AS rowscount ,* FROM {0} WHERE {3} ORDER BY {1} {2}", tablename, order, direction, where);
            }
        }
        else
        {
            if (string.IsNullOrEmpty(where))
            {
                sql = string.Format("SELECT TOP({3})   ROW_NUMBER() OVER(ORDER BY {1} DESC) AS rowscount ,* FROM {0} ORDER BY {1} {2}", tablename, order, direction, top);
            }
            else
            {
                sql = string.Format("SELECT TOP({4})   ROW_NUMBER() OVER(ORDER BY {1} DESC) AS rowscount ,* FROM {0} WHERE {3} ORDER BY {1} {2}", tablename, order, direction, where, top);
            }
        }
        if (bllCommon.GetDataTableBySql(sql).Tables.Count > 0)
        {
            DataTable dtDescription = new DataTable();
            DataTable dtParameter = new DataTable();
            dtDescription = bllCommon.DatabaseTable(tablename).Tables[0];//获取指定表结构及相应字段的说明
            dtParameter = bllCommon.GetDataTableBySql(sql).Tables[0];//获取数据
            InfoView += "<div class=\"th\" style=\"background-color:#F3F7FF;\" align=\"center\"><strong style=\"color: #6774A8;font-size:20px;\">模块信息预览</strong></div><table><tr>";
            for (int colNum = 0; colNum < dtDescription.Rows.Count; colNum++)
            {
                InfoView += "<td align=\"center\">" + dtDescription.Rows[colNum]["字段说明"].ToString() + "</td>";
            }
            InfoView += "</tr>";
            for (int i = 0; i < dtParameter.Rows.Count; i++)
            {
                InfoView += "<tr>";
                for (int j = 1; j < dtParameter.Columns.Count; j++)
                {
                    InfoView += "<td>" + dtParameter.Rows[i][j].ToString() + "</td>";
                }
                InfoView += "</tr>";
                
            }
            InfoView += "</table>";
        }
    }
    #endregion

}
